package com.my_library_one.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.AsyncTask;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.my_library_one.util.internal.BlurTransformation;
import com.my_library_one.util.internal.GlideCircleBorderTransform;
import com.my_library_one.util.internal.GlideCircleTransform;
import com.xiaweizi.cornerslibrary.CornersProperty;
import com.xiaweizi.cornerslibrary.RoundCornersTransformation;

import java.io.IOException;

/**
 * 加载本地和网络图片
 */
public class ImageLoaderUtil {

    //毛玻璃、高斯
    public static void loadBlurTransImg(ImageView v, String url,int radius) {
        RequestOptions options = getRequestOptions();
        options.bitmapTransform(new BlurTransformation(v.getContext(), radius));

        Glide.with(v.getContext())
                .load(url)
                .apply(options)
                .into(v);
    }

    /**
     * 显示普通图片
     * @param v
     * @param url
     */
    public static void loadingUrl(ImageView v, String url) {
        RequestOptions options = getRequestOptions();
        Glide.with(v.getContext())
                .load(url)
                .apply(options)
                .into(v);
    }

    /**
     * 显示圆角图片
     * @param v
     * @param url
     * @param radiu
     * 圆角半径
     * @param type
     * 控制显示哪个角
     */
    public static void loadImgGlideRound(ImageView v, String url,float radiu,CornersProperty.CornerType type) {
        CornersProperty cornersProperty = new CornersProperty();
        cornersProperty.setCornersRadius(DisplayUtil.dip2px(v.getContext(),radiu));
        cornersProperty.setCornersType(type);
        RoundCornersTransformation transformation =
                new RoundCornersTransformation(v.getContext(),cornersProperty);
        RequestOptions options = getRequestOptions();
        options.transform(transformation);

        Glide.with(v.getContext())
                .load(url)
                .apply(options)
                .into(v);
    }

    /**
     * 带颜色边框的圆形图片加载
     * @param v
     * @param url
     * @param color
     * @param border
     *
     */
    public static void loadingCircleBorderImg(ImageView v, String url, int color,int border) {
        RequestOptions options = getRequestOptions();
        options.transform(new GlideCircleBorderTransform(v.getContext(),border, v.getContext().getResources().getColor(color)));

        Glide.with(v.getContext())
                .load(url)
                .apply(options)
                .into(v);
    }

    /**
     * 圆形图片加载
     * @param v
     * @param url
     *
     */
    public static void loadingCircleImg(ImageView v, String url) {
        RequestOptions options = getRequestOptions();
        options.transform(new GlideCircleTransform(v.getContext()));

        Glide.with(v.getContext())
                .load(url)
                .apply(options)
                .into(v);
    }

    /**
     * 加载资源图片
     * @param v
     * @param id
     */
    public static void loadLocalImg(ImageView v, int id) {
        RequestOptions options = getRequestOptions();
        Glide.with(v.getContext())
                .load(id)
                .apply(options)
                .into(v);
    }

    public static RequestOptions getRequestOptions(){
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.RESOURCE);//缓存原图
        //默认加载图片、加载错误、
//        options.placeholder(R.drawable.default_avatar)
//                .error(R.drawable.image_error)
//                .fallback(R.drawable.fallback_nodata);
        return options;
    }
    /**
     * 清除内存缓存.
     */
    private void clearMemoryCache(Context context){
        // This method must be called on the main thread.
        Glide.get(context).clearMemory();
    }

    /**
     * 清除磁盘缓存.
     */
    private void clearDiskCache(final Context context){
        new AsyncTask<Void,Void,Void>(){

            @Override
            protected Void doInBackground(Void... params) {
                // This method must be called on a background thread.
                Glide.get(context).clearDiskCache();
                return null;
            }
        };
    }

    public static int readPictureDegree(String var0) {
        short var1 = 0;

        try {
            ExifInterface var2 = new ExifInterface(var0);
            int var3 = var2.getAttributeInt("Orientation", 1);
            switch (var3) {
                case 3:
                    var1 = 180;
                    break;
                case 6:
                    var1 = 90;
                    break;
                case 8:
                    var1 = 270;
            }
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return var1;
    }

    public static Bitmap rotateImageView(int var0, Bitmap var1) {
        Matrix var2 = new Matrix();
        var2.postRotate((float) var0);
        Bitmap var3 = Bitmap.createBitmap(var1, 0, 0, var1.getWidth(), var1.getHeight(), var2, true);
        return var3;
    }
}
